#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.1415926
typedef enum
{
	OK = 1,
	ERROR = 0
} Status;

Status wave(float fpeak, float dt, float nt, float **amp)
{
	int it;
	float T, t, temp;
	*amp = (float *)malloc(sizeof(float) * nt);
	if (!(*amp))
		return ERROR;
	T = 1. / fpeak;
	//T = 20. / fpeak;
	for (it = 0; it < nt; it++)
	{
		t = it * dt - T;
		temp = PI * fpeak * t;
		temp *= temp;
		(*amp)[it] = (1 - 2 * temp) * exp(-temp);
	}
	return OK;
}

int main()
{
	int it;
	FILE *fp = fopen("../par/brick.txt", "r");
	float fpeak, dt, nt;
	float *amp;
	char fout[100];
	if (!fp)
		return -1;
	fscanf(fp, "fpeak=%f\n", &fpeak);
	fscanf(fp, "nt=%f\n", &nt);
	fscanf(fp, "dt=%f\n", &dt);
	fscanf(fp, "fout=%s\n", fout);
	fclose(fp);
	if (!wave(fpeak, dt, nt, &amp))
		return -1;
	fp = fopen(fout, "wb");
	if (!fp)
		return -1;
	fwrite(amp,sizeof(float),nt,fp);
	fclose(fp);
	free(amp);
	return 0;
}
